// 保存vue路由初始化代码

import {
  createRouter,
  createMemoryHistory,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import routers from "./routes";
import store from "src/store/index";
import Layout from "layouts/MainLayout.vue";
const whiteList = ["/login"]; // 白名单，无需登录的页面

/*
 * If not building with SSR mode, you can
 * directly export the Router instantiation;
 *
 * The function below can be async too; either use
 * async/await or return a Promise which resolves
 * with the Router instance.
 */
const createHistory = process.env.SERVER
  ? createMemoryHistory
  : process.env.VUE_ROUTER_MODE === "history"
  ? createWebHistory
  : createWebHashHistory;

let createRoute = () =>
  createRouter({
    scrollBehavior: () => ({ left: 0, top: 0 }),
    routes: routers,

    // Leave this as is and make changes in quasar.conf.js instead!
    // quasar.conf.js -> build -> vueRouterMode
    // quasar.conf.js -> build -> publicPath
    history: createHistory(
      process.env.MODE === "ssr" ? void 0 : process.env.VUE_ROUTER_BASE
    ),
  });
// if (to.matched.length == 0) { router.push(to.path); }

const Router = createRoute();

export function resetRouter() {
  const newRouter = createRoute();
  Router.matcher = newRouter.matcher; // reset router
}
export default Router;
// export default route(function (/* { store, ssrContext } */) {

// 看板新增路由刷新不丢失
// if (localStorage.getItem("euWorkShop_boardViewRouterArr") != null) {
//   let data = JSON.parse(localStorage.getItem("euWorkShop_boardViewRouterArr"));
//   let path = {
//     path: "/board",
//     component: Layout,
//     redirect: "/board/oee/index",
//     name: "Board",
//     meta: {
//       title: "看板展示 board",
//       fixedMenu: "看板",
//       affix: true,
//       icon: "mdi-calendar-clock",
//     },
//     children: [
//       {
//         path: "/boardOEE",
//         component: () => import("pages/board/oee/index"),
//         name: "BoardOEE",
//         meta: {
//           title: "效率排行榜",
//           show: 1,
//           oneMenuShow: 1,
//           icon: "mdi-circle-medium",
//         },
//       },
//       {
//         path: "/boardAerial",
//         component: () => import("pages/board/aerial/index"),
//         name: "BoardAerial",
//         meta: {
//           title: "鸟瞰看板",
//           show: 1,
//           oneMenuShow: 1,
//           icon: "mdi-circle-medium",
//         },
//       },
//       {
//         path: "/" + data.url,
//         name: data.url,
//         component: () => import("pages/board/boardPreview/index.vue"),
//         meta: {
//           title: data.name,
//         },
//       },
//     ],
//   };
//   Router.addRoute(path);
// }
// 路由守卫
Router.beforeEach(async (to, from, next) => {
  if (to.path === "/dashboard") {
    next("/realTimeState");
  } else {
    next();
  }

  // 判断用户是否登录
  // const hasToken = localStorage.getItem("token_key_GFYW");
  // const localVueVersion = localStorage.getItem("vueVersion");
  // if (localStorage.getItem("euworkShop_goViewColor") == null) {
  //   const colorArr = {
  //     color1: "#51d6a9",
  //     color2: "#2a3e38",
  //     color3: "#3a6255",
  //     color4: "rgba(53, 90, 78, 0.2)",
  //   };
  //   localStorage.setItem("euworkShop_goViewColor", JSON.stringify(colorArr));
  // }
  // // 判断是否是预览以下的页面
  // let str = to.path.substr(0, 13);

  // if (
  //   to.path != "/User" ||
  //   to.path != "/goview" ||
  //   to.path != "/echarts" ||
  //   to.path != "/userLookPreview" ||
  //   str != "/boardPreview"
  // ) {
  //   store.dispatch("leftChart/overPathFunc");
  // }

  // if (to.path === "/wechat") {
  //   next();
  // } else if (to.path === "/userLookPreview") {
  //   next();
  // } else if (to.path === "/user") {
  //   next();
  // } else {
  //   if (hasToken) {
  //     if (to.path === "/login") {
  //       next("/realTimeState");
  //     } else {
  //       if (to.path === "/a") {
  //         next();
  //         return;
  //       }
  //       if (to.path === "/errorPermission") {
  //         next();
  //         return;
  //       }
  //       if (str == "/boardPreview") {
  //         next();
  //         return;
  //       }
  //       try {
  //         //判断当前代码版本是否与服务器中代码版本一致，如不一致则刷新页面获取最新 / 是否有权限
  //         const { permissions, version } = await store.dispatch(
  //           "login/getPagePremission",
  //           { page_url: to.path }
  //         );
  //         if (localVueVersion && localVueVersion != version) {
  //           localStorage.setItem("vueVersion", version);

  //           if (permissions) {
  //             next();
  //           } else {
  //             if (to.path === "/dashboard") {
  //               next("/realTimeState");
  //             } else {
  //               next("/errorPermission");
  //             }
  //           }
  //           setTimeout(() => {
  //             window.location.reload();
  //           }, 500);
  //         } else {
  //           localStorage.setItem("vueVersion", version);

  //           if (permissions) {
  //             next();
  //           } else {
  //             if (to.path === "/dashboard") {
  //               next("/realTimeState");
  //             } else {
  //               next("/errorPermission");
  //             }
  //           }
  //         }
  //       } catch (error) {
  //         store.dispatch("login/logoutFun");
  //         store.commit("app/SET_VISITED_VIEWS"); //  清空可见tab标签
  //         next("/login");
  //       }
  //     }
  //   } else {
  //     if (whiteList.indexOf(to.path) !== -1) {
  //       next();
  //     } else {
  //       next("/login");
  //     }
  //   }
  // }
});
